package com.wframe.project.former.dataservices.floor;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;

import com.frame.database.JBaseDao;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;

public class FloorDao extends JBaseDao{

	public FloorDao() {
		super("wb_floor");
	}

	public void delFloor(String id) throws Exception{
		del(id);
		getSession().update("delete from wb_floor_item_products where itemid in (select id from wb_floor_item where floorid=?)",new Object[]{id});
		getSession().update("delete from wb_floor_item where floorid=?",new Object[]{id});
	}
	
	public List<JdataBean> queryFloorItem(String floorid) throws Exception{
		return getSession().query("select * from wb_floor_item where floorid=? order by orderno is null asc,orderno,id", new Object[]{floorid});
	}
	
	public JdataBean getFloorItem(String id) throws Exception{
		return getSession().getObject("select * from wb_floor_item where id=?", new Object[]{id});
	}
	
	public String saveFloorItem(JdataBean itembean) throws HibernateException, Exception{
		if(StringUtils.isNotBlank(itembean.getString("id"))){
			Map key=new HashMap();
			key.put("id", itembean.getString("id"));
			getSession().update("wb_floor_item",itembean.getDataMap(),key);
			return itembean.getString("id");
		}else
			return getSession().saveReturnKey("wb_floor_item",itembean.getDataMap());
	}
	
	public void delFloorItem(String id) throws Exception{
		getSession().update("delete from wb_floor_item_products where itemid=?",new Object[]{id});
		getSession().update("delete from wb_floor_item where id=?",new Object[]{id});
	}
	
	public void delFloorItemProducts(String itemid) throws Exception{
		getSession().update("delete from wb_floor_item_products where itemid=?",new Object[]{itemid});
	}

//	public void saveFloorItemProducts(String itemid,String[] products) throws Exception{
//		if(products!=null){
//			for(String pid:products){
//				String[] ss=pid.split("_");
//				getSession().update("insert into wb_floor_item_products(itemid,productid,priceid,orderno) values (?,?,?)",new Object[]{itemid,ss[0],ss[1]});
//			}
//		}
//	}

	public void saveFloorItemProducts(String itemid,String[] products) throws Exception{
		if(products!=null){
			for(int i=0;i<products.length;i++){
				String[] ss=products[i].split("_");
				getSession().update("insert into wb_floor_item_products(itemid,productid,priceid,orderno) values (?,?,?,?)",new Object[]{itemid,ss[0],ss[1],i});
			}
		}
	}

	public PageResult queryFloorProduct(JdataBean parms,int size,int pageno) throws Exception{
		String sql="select t.*,CONCAT(p.id,'_',t.id) idcode,p.unitname,p.code productcode,p.name productname,p.attrvals,s.filepath photourl,s.viewpath photoview,sm.filepath product_photourl,sm.viewpath product_photoview"
				+ " from wb_floor_item_products f"
				+" inner join wb_products_prices t on t.id=f.priceid"
				+" inner join wb_products p on p.id=f.productid"
				+" left join app_pictures s on s.id=t.photo"//销售属性主图片
				+" left join app_pictures sm on sm.id=p.photo"//商品主图片
				+" where p.status<>-1";
		List parmlist=new ArrayList();
		String order="";
		if(parms!=null){
			if(StringUtils.isNotBlank(parms.getString("itemid"))){
				sql+=" and f.itemid=?";
				parmlist.add(parms.getString("itemid"));
			}
		}
		if(StringUtils.isNotBlank(order))sql+=" order by "+order;
		else sql+=" order by f.orderno";
		PageResult pr= getSession().query2(sql, parmlist, size, pageno);
		List<JdataBean> productlist=pr.getList();
		for(JdataBean product:productlist){
			String photo=product.getString("photoview");
			if(StringUtils.isBlank(photo))photo=product.getString("product_photoview");
			product.put("photo", photo);
			product.put("fullname", product.getString("productname")+" "+product.getDefault("name","").toString());
		}
		return pr;
	} 
	public void updatestatus(String floorid,int status) throws Exception{
		getSession().update("update wb_floor set status=? where id=?", new Object[]{status,floorid});
	}
}
